/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import Entity.Transaction;
import Entity.Address;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import session.SalesLeadSessionLocal;
import Entity.SalesLead;

/**
 *
 * @author cheeseng
 */
@ManagedBean(name = "saleLeadMB")
@RequestScoped
public class SaleleadMB {

    /** Creates a new instance of SaleleadMB */
    @EJB
    private SalesLeadSessionLocal saleLeadBean;
    private long leadId;
    private String statusMessage;
    private String salutation;
    private String contactPerson;
    private String company;
    private String phone;
    private String email;
    private String description;
    private String comAddress1;
    private String comAddress2;
    private String comPostalCode;
    private String comCountry;
    
    public SaleleadMB() {
    }
    
    public ArrayList<SalesLead> getAllLeads() throws Exception {
        return saleLeadBean.getAllLeads();
        
        
    }
    
    public String getCompany() {
        return company;
    }
    
    public String getComAddress1() {
        return comAddress1;
    }
    
    public void setComAddress1(String comAddress1) {
        this.comAddress1 = comAddress1;
    }
    
    public String getComAddress2() {
        return comAddress2;
    }
    
    public void setComAddress2(String comAddress2) {
        this.comAddress2 = comAddress2;
    }
    
    public String getComCountry() {
        return comCountry;
    }
    
    public void setComCountry(String comCountry) {
        this.comCountry = comCountry;
    }
    
    public String getComPostalCode() {
        return comPostalCode;
    }
    
    public void setComPostalCode(String comPostalCode) {
        this.comPostalCode = comPostalCode;
    }
    
    public void setCompany(String company) {
        this.company = company;
    }
    
    public String getContactPerson() {
        return contactPerson;
    }
    
    public void setContactPerson(String contactPerson) {
        this.contactPerson = contactPerson;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    public long getLeadId() {
        return leadId;
    }
    
    public void setLeadId(long leadId) {
        this.leadId = leadId;
    }
    
    public String getPhone() {
        return phone;
    }
    
    public void setPhone(String phone) {
        this.phone = phone;
    }
    
    public SalesLeadSessionLocal getSaleLeadBean() {
        return saleLeadBean;
    }
    
    public void setSaleLeadBean(SalesLeadSessionLocal saleLeadBean) {
        this.saleLeadBean = saleLeadBean;
    }
    
    public String getSalutation() {
        return salutation;
    }
    
    public void setSalutation(String salutation) {
        this.salutation = salutation;
    }
    
    public String getStatusMessage() {
        return statusMessage;
    }
    
    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public void saveNewLead(ActionEvent event) {
        try {
            if (saleLeadBean.checkConflictLead(company)) {
                
                throw new Exception("Duplicate sale lead record exists. cannot be saved as new record.");
                
            } else {
                
                leadId = saleLeadBean.createLead(salutation, contactPerson, company, comAddress1, comAddress2, comPostalCode, comCountry, phone, email, description);
                
                statusMessage = "New sale lead is saved successfully.";
                //    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Create New Sale Lead Result: " + statusMessage + " (New Sale Lead ID is " + leadId + ")", ""));
            }
            
        } catch (Exception e) {
            
            statusMessage = e.getMessage();
            leadId = -1L;
            //   FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Add New Sale Lead Result: " + statusMessage, ""));
        }
        
    }
    
    public void updateSaleLead() {
        
        try {
            saleLeadBean.updateLead(leadId, salutation, contactPerson, phone, email, description);
            statusMessage = "sale lead is updated successfully";
        } catch (Exception e) {
            statusMessage = e.getMessage();
        }
        
    }
    
    public void removeSaleLead() {
        try {
            saleLeadBean.removeLead(leadId);
            statusMessage = "Sale lead record is deleted successfully";
            
            
            
        } catch (Exception e) {
            statusMessage = e.getMessage();
        }
    }
}
